
function getToken(){
    var token='';
    try {
        token=  JSON.parse(window.localStorage.getItem("user")).token;
    }catch (e) {
        token='';
    }
    return token;
}
function getAction(url, param, callback) {

    if(window.$){
        $.ajax({
            url: url,
            data: param,
            method: "GET",
            headers:{'token':getToken()},
            success: function (res) {
                if (checkCode(res)) {
                    callback && callback(data);
                }
            }
        })
    }else if (axios) {
        axios({
            method: "get",
            url: url,
            params: param,
        }).then(function (res) {
            let data=res.data;
            if (checkCode(data)) {
                callback && callback(data);
            }
        })
    }

}

function postAction(url, param, callback) {
    if(window.$){
        $.ajax({
            url: url,
            data: param,
            method: "POST",
            headers:{'token':getToken()},
            success: function (res) {
                if (checkCode(res)) {
                    callback && callback(data);
                }
            }
        });
    }else if (axios) {
        axios({
            method: "post",
            url: url,
            params: param,
        }).then(function (res) {
            let data=res.data;
            if (checkCode(data)) {
                callback && callback(data);
            }

        })
    }
}
function jsonAction(url, param, callback) {
    if(window.$){
        $.ajax({
            url: url,
            data: JSON.stringify(param),
            method: "POST",
            contentType: 'application/json',
            headers:{'token':getToken()},
            success: function (res) {
                if (checkCode(res)) {
                    callback && callback(data);
                }
            }
        })
    }else if (axios) {
        axios({
            method: "post",
            url: url,
            data: param,
            headers: {
                'Content-Type': 'application/json'
            },
        }).then(function (res) {
            let data=res.data;
            if (checkCode(data)) {
                callback && callback(data);
            }
        })
    }

}
function checkCode(res){
    if (res.code != 200) {
        if (top&&top.layer) {
            top.layer.msg(res.msg)
            return false
        }
        console.error(res.msg);
    }
    return true;

}
